package edu.washington.cs.cmdsynth;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import plume.Pair;

import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation;
import edu.stanford.nlp.util.CoreMap;
import edu.washington.cs.cmdsynth.utils.Utils;

public class CommandParser {

	private NLPEngine engine = new NLPEngine();
	
	private String cmd = null;
	
	public void setCommand(String cmd) {
		Utils.checkNotNull(cmd);
		this.cmd = cmd;
	}
	
	public void parse() {
		Utils.checkNotNull(this.cmd);
		engine.parseText(this.cmd);
	}
	
	//FIXME should not use String, String
	public List<Pair<String, String>> extractVerbObjectPairs() {
		List<Pair<String, String>> pairList = new LinkedList<Pair<String, String>>();
		
		List<CoreMap> sentences = engine.getParsedSentences();
		for(CoreMap sentence : sentences) {
			SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
			Set<IndexedWord> set = dependencies.vertexSet();
		      for(IndexedWord w : set) {
		    	  if(w.tag().equals(Tags.VB)) {
		    		  List<SemanticGraphEdge> edges = dependencies.getOutEdgesSorted(w);
			    	  for(SemanticGraphEdge edge : edges) {
			    		  if(edge.getRelation().getShortName().equals(Relations.dobj)) {
			    			  pairList.add(Pair.of(w.value(), edge.getTarget().value()));
			    		  }
			    	  }
		    	  }
		      }
		}
		
		return pairList;
	}
	
	/***
	 * Basic idea: 
	 * 1. extract det relation:  all files  , or  the files
	 * 2. CC conjunction. divide it into two parts.  and then use co-ref to connect (low priority)
	 *    then, before, after
	 * 3. extract prep , e.g., all files in the current directory (in)
	 * 4. pobj == in the directory, the relation between in and directory	
	 * 5. amod  (red meat), all accessible files
	 * 6. nn  (compound modifier).  e.g., java files
	 * short cut ==> java files => *.java
	 * 
	 * 7. auto correction ==> list files in dir, the list might be annotated as nn
	 * 
	 * 8  long list files in curr dir (long is RB (adv)
	 * 
	 * a dictionary
	 * 
	 * the language:   action -- option  inputs  |  action -- option inputs
	 * 
	 * DT : determinter (all, a, the)
	 * 
	 * VBG/VBN, tenses of the verb (analyze dep)
	 * */
}